package com.dapeng.seria;


import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class HessianSerializerExample {

    public static void main(String[] args) throws Exception {

        // 创建一个对象
        Person person = new Person(1, "John");

        // 使用 Hessian 进行序列化
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HessianOutput hessianOutput = new HessianOutput(baos);
        hessianOutput.writeObject(person);
        hessianOutput.close();

        byte[] bytes = baos.toByteArray();
        System.out.println("序列化长度: " + bytes.length);

        // 使用 Hessian 进行反序列化
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        HessianInput hessianInput = new HessianInput(bais);
        Person deserializedPerson = (Person) hessianInput.readObject();
        hessianInput.close();

        System.out.println("反序列化后的对象：" + deserializedPerson.getName() + ", " + deserializedPerson.getId());
    }
}
